<template>
	<view class="flex-col justify-center items-center page">
	  <view class="flex-col justify-start items-center">
	    <image src="https://jianji.axu9.com/friendCircleImg/creating-gif.gif" style="width: 750rpx;" mode="widthFix"/>
	  </view>
	  <text class="text">计划生成中...</text>
	</view>
</template>

<script>
	import {
		createTask
	} from '../../haoduogeni/common/api/send';
	import {
		imgJindu
	} from '../../haoduogeni/common/api/user';
	export default {
		data() {
			return {
				code: '',
				    timerCheckCode: null,
				    uid: ''
			}
		},
		onLoad(options) {
			// #ifdef MP-WEIXIN
			// 开启返回提示
			uni.enableAlertBeforeUnload({
			  message: '正在生成计划，请勿离开当前页面',
			  success: function(res) { console.log('成功开启提示'); },
			  fail: function(res) { console.log('失败'); }
			});
			// #endif
			//AI计划
			this.code = options.code;
			this.uid = options.uid;
			this.getStatusByCode();
			
		},
		beforeDestroy() {
		    clearTimeout(this.timerCheckCode);
		  },
		methods: {
			getStatusByCode(){
			    // uni.showLoading({
			    //   title: '生成中',
			    // })
			    var _this = this;
			      imgJindu({
			        code: this.code,
			        pt_id: this.$ptId,
			        uid: uni.getStorageSync('userId'),
			      }).then(res => {
			        //成功时回调函数
			        console.log(res, '检测状态')
			        if (res.errno == 0) {
			          // _this.bindUserByuid(res.data);
			          console.log(res.data,'res.data')
			          var data = res.data;
			          const allStatusValid = data.every(item => item.status === 1);
			          if(allStatusValid){
			            console.log('停止检测状态')
			            uni.hideLoading()
			            var arr1 = []
			            data.forEach(item => {
			              arr1.push({
			                title: item.text,
			                imgs: item.img.map(item => ({ url: item.img,id: item.id })),
			                publishType: "1", //1立即发布 2定时发布
			                publishDate: "", //定时发布日期
			                publishTime: "", //定时发布时间
			                uploadType: "1", //1图片 2视频
			                time: "", //循环发布时间
			                num: "", //循环发布次数
			                editorRef: null, //编辑器实例
			                release_promotion: "0", //0正常发布 1二维码发布
			                release_promotion_type: "1", //1图片 2链接
			                selectedKey: null,
			                selectedKeyImg: null,
			                publishLoop: "1", //定时发布是否循环 0是 1否\
			                imgsPoster: [], //海报
			              })
			            })
			            uni.redirectTo({
			              url: '/haoduogeni/createPlan/index?obj=' + JSON.stringify(arr1) + '&uid=' + this.uid,
			            })
			          }else {
			            _this.timerCheckCode = setTimeout(() => {
			              _this.getStatusByCode();
			            }, 1000);
			          }
			        }else {
			          // uni.hideLoading()
			        }
			      }).catch(err => {
			        //失败时回调函数
			        console.log(err)
			        // uni.hideLoading()
			      })
			  },
			  createPlan(params) {
			    var _this = this;
			    createTask(params).then(res => {
			      //成功时回调函数
			      console.log(res, 'getWxUser')
			      if (res.errno == 0) {
			        uni.showToast({
			          title: '创建成功',
			          icon: 'none',
			          duration: 1000
			        })
			        setTimeout(() => {
			          uni.navigateTo({
			            url: '/haoduogeni/planList/index?uid=' + params.uid,
			          })
			        }, 1000)
			      } else {
			        uni.showToast({
			          title: res.msg,
			          icon: 'none'
			        })
			      }
			    }).catch(err => {
			      //失败时回调函数
			      console.log(err)
			    })
			
			  },
		}
	}
</script>

<style lang="less" scoped>
	@import url("../common/css/common.css");
	page {
	  background-color: #ffffff;
	}
	.page {
	  /* padding: 455rpx 0 606rpx; */
	  
	  width: 100vw;
	  height: 90vh;
	}
	/* .text-wrapper {
	  padding: 443rpx 0 86rpx;
	  background-image: url('https://jianji.axu9.com/friendCircleImg/creating-gif.gif');
	  background-size: 100% 100%;
	  background-repeat: no-repeat;
	} */
	.text {
	  color: #000000;
	  font-size: 36rpx;
	  font-family: Chiron GoRound TC;
	  font-weight: 500;
	  line-height: 34rpx;
	  margin-top: -90rpx;
	}

</style>
